package com.start_eternity.oasissafewalk.controller;

import com.start_eternity.oasissafewalk.pojo.Account;
import com.start_eternity.oasissafewalk.pojo.PageBean;
import com.start_eternity.oasissafewalk.pojo.Result;
import com.start_eternity.oasissafewalk.service.AccountService;
import com.start_eternity.oasissafewalk.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.hibernate.validator.constraints.URL;

import java.time.LocalDate;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @GetMapping
    public Result accountResult(@RequestHeader(name = "token") String token){
        //根据用户名查询用户
        Map<String,Object> map = JwtUtils.parseJWT(token);
        String username = (String) map.get("username");
        Account account = accountService.getByName(username);
        return Result.success(account);
    }
    //分页查询
    @GetMapping("/all")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize,
                       String username, String password,String name,Integer gender,String accountPic,String number,
                       @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern="yyyy-MM-dd")LocalDate end){
        log.info("分页查询,参数:{},{},{},{},{},{},{},{},{}",page,pageSize,username,password,name,gender,accountPic,number,begin,end);
        //调用service方法进行分页查询
        PageBean pageBean= accountService.page(page,pageSize,username,password,name,gender,accountPic,number,begin,end);
        return Result.success(pageBean);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Account account) {
        log.info("更新车辆信息：{}",account);
        accountService.update(account);
        return Result.success();
    }
}
